<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <style>
            /* * {
            margin: 0;
            padding: 0;
        } */
            ul {
                list-style: none;
            }
            ul li {
                height: 48px;
                width: 540px;
            }
            input {
                height: 40px;
                font-size: 20px;
            }
            ul li input {
                margin-right: 10px;
                width: 120px;
            }
            input.submit {
                margin-left: 40px;
                width: 80px;
                background-color: greenyellow;
                border: 1px solid green;
                transition: all 0.3s;
                border-radius: 5px;
                cursor: pointer;
            }
            input.submit:hover {
                background-color: rgb(137, 202, 39);
            }
        </style>
    </head>
    <body>
        <ul>
            <li>
                <input type="number" placeholder="学分" /><input
                    type="number"
                    placeholder="成绩"
                />
            </li>
        </ul>
        <input type="submit" value="计算" class="submit calc" />
        <input type="submit" value="添加行" class="submit add" /><br><br>
        <input type="submit" value="清除记录" class="submit clear" />
        <div class="return"></div>
        <script>
            let calc = document.querySelector(".calc");
            let addBtn = document.querySelector(".add");
            let clearBtn = document.querySelector(".clear");
            let ul = document.querySelector("ul");
            let newli = ul.children[0].cloneNode(1);
            calc.addEventListener("click", function () {
                let div = document.querySelector(".return");
                let scoreSum = 0;
                let creditSum = 0;
                let result = 0;
                for (let i = 0; i < ul.children.length; i++) {
                    if (ul.children[i].children[0].value) {
                        credit = parseInt(ul.children[i].children[0].value);
                        score = ul.children[i].children[1].value * credit;
                        creditSum += credit;
                        scoreSum += score;
                        localStorage.setItem(credit,score);
                    }
                }

                result = scoreSum / creditSum;
                div.innerHTML = result.toFixed(2);
            });
            clearBtn.addEventListener("click",function(){
                localStorage.clear();
            })
            addBtn.addEventListener("click", function () {
                let cloneli = newli.cloneNode(1);
                ul.append(cloneli);
            });
        </script>
    </body>
</html>
